<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>新增任务</title>
<script src="MyDatePicker/WdatePicker.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="jquery-easyui/themes/icon.css">
<script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui/jquery.easyui.min.js"></script>
</head>
<body>
<ol class="breadcrumb" style="margin-bottom:5px;">
	<li><a href="#">任务系统</a></li>
	<li class="active">创建任务</li>
</ol>
<div id="help"></div>
<a id="toAdd" onclick="add()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增任务</a>
<!-- 4.查询框 div -->
<div id="search" style="padding:3px">
	<span>任务编号:</span>
	<input id="missionId" style="line-height:26px;border:1px solid #ccc">
	<span>任务名称:</span>
	<input id="missionName" style="line-height:26px;border:1px solid #ccc">
	<span>任务内容:</span>
	<input id="missionContent" style="line-height:26px;border:1px solid #ccc">
	<span>任务时间:</span>
	<input id="startTime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" style="line-height:26px;border:1px solid #ccc">
	<span>到</span>
	<input id="endTime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" style="line-height:26px;border:1px solid #ccc">
	<span>任务状态:</span>
		  <select name="missionState" id="missionState" style="width: 100px;height:30px">
	             <option value="" selected="selected">任务状态</option>
	             <option value="未发布" >未发布</option>
	             <option value="已发布">已发布</option>
	             <option value="已完成">已完成</option>
	             <option value="已反馈">已汇报</option>
	             <option value="已取消">已取消</option>
	       </select> 
	<a  href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="doSearch()">查询</a>
</div>
<!-- 任务列表 -->
<table id="dg" class="easyui-datagrid" style="width:auto; height:500px;"  striped="true" toolbar="#search" rownumbers="true"  fitcolumns="true" pagination="true" singleSelect="true"></table>
<!--1.新增框 div-->
<div id="doAdd" class="easyui-dialog" title="新建任务" closed="true" style="width:350px;height:400px;" data-options="iconCls:'icon-save',resizable:true,modal:true">   
	<form  id="addMission" style="padding:10px 20px 10px 40px;">
		<p>任务编号: <input type="text" class="form-control" id="m_id" name="m_id"  readonly="readonly" placeholder="任务编号" ></p>
		<p>任务名称: <input type="text" class="form-control" id="m_name" name="m_name"  placeholder="任务名称" ></p>
		<p>任务内容: <textarea  class="form-control" id="m_content" name="m_content" placeholder="任务内容" ></textarea></p>
		<p>截止时间: <input type="text" class="form-control" id="m_end_date" name="m_end_date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" placeholder="截止时间"/></p>
		<div style="padding:5px;text-align:center;">
			<a onclick="addMission()" class="easyui-linkbutton" icon="icon-ok">添加</a>
			<a onclick="cancel1()" class="easyui-linkbutton" icon="icon-cancel">取消</a>
		</div>
	</form>
</div>
<!--2.修改框 div-->
<div id="doupdate" class="easyui-dialog"  title="修改任务"  closed="true" style="width:350px;height:400px" data-options="iconCls:'icon-save',modal:true">   
	<form id="upMission" style="padding:10px 20px 10px 40px;">
		<p>任务编号: <input type="text" class="form-control" id="m_id1" name="m_id" readonly="readonly" placeholder="任务编号" ></p>
		<p>任务名称: <input type="text" class="form-control" id="m_name1" name="m_name"  placeholder="任务名称" ></p>
		<p>任务内容: <textarea  class="form-control" id="m_content1" name="m_content" placeholder="任务内容" ></textarea></p>
		<p>截止时间: <input type="text" class="form-control" id="m_end_date1" name="m_end_date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" placeholder="截止时间"/></p>
		<div style="padding:5px;text-align:center;">
			<a onclick = "upMission()" class="easyui-linkbutton" icon="icon-ok">修改</a>
			<a onclick="cancel2()" class="easyui-linkbutton" icon="icon-cancel">取消</a>
		</div>
	</form>
</div> 
<!--3.发布任务 div-->
<div id="deliver" class="easyui-dialog"  title="发布任务"  closed="true" style="width:600px;height:400px"  data-options="iconCls:'icon-save',modal:true">   
	<div id="cc" class="easyui-layout"  style="width:600px;height:400px;">  
	    <div data-options="region:'east',iconCls:'icon-reload',title:'员工',split:true"  style="width:300px;">
	    	<table id="dg1"></table>
	    </div>   
	    <div data-options="region:'center',title:'任务'" style="padding:5px;background:#eee;">
	        <p>任务编号: <input type="text" class="form-control" id="m_id2" name="m_id"  readonly="readonly" placeholder="任务编号" ></p>
			<p>任务名称: <input type="text" class="form-control" id="m_name2" name="m_name"  readonly="readonly" placeholder="任务名称" ></p>
			<p>任务内容: <textarea  class="form-control" id="m_content2" name="m_content" readonly="readonly" placeholder="任务内容" ></textarea></p>
			<p>截止时间: <input type="text" class="form-control" id="m_end_date2" name="m_end_date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly"  placeholder="截止时间"  /></p>
	    </div> 
	</div>  
</div>
<script src="MyDatePicker/WdatePicker.js"></script>
<script type="text/javascript">
	$('#dg').datagrid({    
	    url:'MissionToList.do', 
	    pagination:true,
	    fitColumns:true,
	    pageList:[2,4,6,8,10],
	    columns:[[ 
	        {field:'m_id',title:'任务编号',width:100,align:'center'}, 
	        {field:'m_name',title:'任务名称',width:100,align:'center'},
	        {field:'e_id',title:'发起人编号',width:100,align:'center'},
	        {field:'e_name',title:'发起人姓名',width:100,align:'center'},
	        {field:'m_content',title:'任务内容',width:100,align:'center'},
	        {field:'m_start_date',title:'任务创建时间',width:100,align:'center'},
	        {field:'m_end_date',title:'任务截止日期',width:100,align:'center'},
	        {field:'m_state',title:'任务状态',width:100,align:'center'},
	        {field:'link',title:'操作',width:150,align:'center',formatter:function(value,row,rowIndex){
	        	if(row.m_state=="未发布"){
	        		return '<button onclick="return update(\''+row.m_id+'\');">修改</button> <button  onclick="return del(\''+row.m_id+'\')" >删除</button> <button  onclick="return todeliver(\''+row.m_id+'\');" >发布</button>';
	        	}else if(row.m_state=='已发布'){
	        		return '<button  onclick="return todeliver(\''+row.m_id+'\');">发布</button>';
	        	}else if(row.m_state=='已完成'){
	        		return '<button  onclick="return toMissionInfo(\''+row.m_id+'\');">详情</button>';
	        	}
	        }},
	    ]]    
	}); 
	function add(){//点击 新增 按钮时触发
		$('#doAdd').dialog('open');
		var xhr= new XMLHttpRequest();
		var url='MissionToAdd.do';
		xhr.open("get", url, true);
		xhr.onreadystatechange=function(){
			if(this.readyState == 4 && this.status == 200){
				var jsonstr = this.responseText;
				eval(' var jsonstring = '+jsonstr);
				document.getElementById("m_id").value=jsonstring;
			}
		};
		xhr.send();
		return false;
	}
	function addMission(){
		//发送ajax请求
		$.ajax({
			type:"post",
			url:"MissionDoAdd.do",
			dataType:'json',
			data:$("#addMission").serialize(),
			error: function(request) {
	            alert("Connection error");
	        },
	        success: function(data) {
	        	$.messager.alert('消息',data.msg,'info',function(){
	        		window.location.href="Mission.jsp";
	        	});
	        }
		});
	}
	function cancel1(){
		$('#doAdd').dialog('close');
	}
	function del(m_id){
		if(confirm("确定删除吗？")){
			$.ajax({
				type:"get",
				url:"MissionDoDel.do?m_id="+m_id,
				dataType:'json',
				error: function(request) {
		            alert("Connection error");
		        },
		        success: function(data) {
		        	$.messager.alert('消息',data.msg,'info',function(){
		        		window.location.href="Mission.jsp";
		        	});
		        }
			});
		}
		return false;
	}
	function update(m_id){
			$('#doupdate').window('open');
			$.ajax({
				type: "get",
				url: "MissionToUpd.do?m_id="+m_id,
				dataType:'json',
				success: function (data) {
		            $('#m_id1').val(data.m_id);
		            $('#m_name1').val(data.m_name);
		            $('#m_content1').val(data.m_content);
		            $('#m_end_date1').val(data.m_end_date);
		         }		
			});
			return false;
		}
	function cancel2(){
		$('#doupdate').dialog('close');
	}
	function upMission(){
		$.ajax({
	        type: "post",
	        url:"MissionDoUpd.do",
	        dataType:'json',
	        data:$('#upMission').serialize(),
	        error: function(request) {
	            alert("Connection error");
	        },
	        success: function(data) {
	        	$.messager.alert('消息',data.msg,'info',function(){
	        		window.location.href="Mission.jsp";
	        	});
	        }
	    });
		return false;
	}
	function todeliver(m_id,e_id){
		$.ajax({
			type: "get",
			url:"MissionToDeliver.do",
			dataType:'json',
			data:{m_id:m_id},
			success:function(data){
				console.log(data);
			      $('#m_id2').val(data.dto.m_id);
		          $('#m_name2').val(data.dto.m_name);
		          $('#m_content2').val(data.dto.m_content);
		          $('#m_end_date2').val(data.dto.m_end_date);
		          $.ajax({
		        	  url:'MissionToEmp.do', 
		        	  dataType:'json',
		        	  success:function(list){
		        		  console.log(list);
		        		  $('#deliver').window('open');
		        		  $('#dg1').datagrid({    
		      			    fitColumns:true,
		      			    columns:[[    
		      			        {field:'e_id',title:'员工编号',width:100,align:'center'},
		      			        {field:'e_name',title:'员工姓名',width:100,align:'center'},    
		      			      	{field:'d_name',title:'部门名称',width:100,align:'center'}, 
		      			        {field:'link',title:'操作',width:150,align:'center',formatter:function(value,row,rowIndex){
		      			        	return '<button  onclick="return doDeliver(\''+row.e_id+'\');" >派发</button> ';
		      			        }},
		      			    ]],
		      			   data:list
		      			}); 
		        	  }
		          });
			},
		});
		return false;
	}
	function doDeliver(e_id){
		var m_id = document.getElementById("m_id2").value;
		$.ajax({
			url:"MissionDoDeliver.do",
			dataType:'json',
			data:{
				'm_id':m_id,
				'e_id':e_id,
			},
			success:function(data){
				$.messager.alert('消息',data.msg);
				$('#dg').datagrid('load',{
					code: '01',
				});
			}
		});
		return false;
	}
	function doSearch(){
		$('#dg').datagrid('load',{
			m_id: $('#missionId').val(),
			m_name: $('#missionName').val(),
			m_content:$('#missionContent').val(),
			m_start_date: $('#startTime').val(),
			m_end_date: $('#endTime').val(),
			m_state: $('#missionState').val()
		});
	}
</script>
</body>
</html>